// pages/common/ticketEdit/ticketEdit.js
import {
  ui
} from '../../../tools/ui'
import {
  checkTool
} from '../../../tools/check'
import {
  pages
} from '../../../tools/pages'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    values: {
      unit: 10,
      gift: 2,
      image: ''
    }
  },
  onTapValue(e) {
    const name = e.currentTarget.dataset.name
    const {
      values
    } = this.data
    values[name] = e.detail.value
    this.setData({
      values
    })
  },
  selectNum(name) { // not used
    const items = (name == 'unit' ? ['5', '10', '15', '20', '25', '30'] : ['1', '2', '3', '4', '5'])

    ui.actionSheet(items, res => {
      const values = this.data.values
      values[name] = res
      this.setData({
        values
      })
    })
  },
  imgClick() {
    wx.navigateTo({
      url: '../imgSelect/index',
    })
  },
  imgSelected(files, tag) {
    const values = this.data.values
    values.image = files[0]
    this.setData({
      values
    })
  },
  onDel() {
    pages.back(prePage => {
      prePage.ticketCallback({
        deleted: true,
        image: ""
      });
    })
  },
  onSave() {
    const values = this.data.values
    if (!checkTool.check([{
        value: values.image,
        title: '主图'
      }, {
        value: values.unit,
        regex: checkTool.CommonRegex.integer,
        title: '购买数量'
      }, {
        value: values.gift,
        regex: checkTool.CommonRegex.integer,
        title: '赠送数量'
      }])) return

    pages.back(prePage => {
      values.deleted = undefined
      prePage.ticketCallback(values);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.ticket) {
      this.setData({
        values: JSON.parse(options.ticket)
      })
    }
  },




})